package com.jilefojing.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.Expose;
import com.jilefojing.beans.pojos.enhanced.EnhancedComment;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.service.ICommentService;
import com.jilefojing.service.IExposeService;
import com.jilefojing.web.base.BaseWeb;
import com.jilefojing.web.convent.CommonConvent;

/**
 * 举报
 * 
 * @author jiangguoshun
 *
 */
@Controller
@RequestMapping("expose")
public class ExposeWeb extends BaseWeb {
	@Autowired
	private IExposeService exposeService;
	@Autowired
	private ICommentService commentService;

	@RequestMapping("template-{itemType}-{itemId}.html")
	public String getTemplate(@PathVariable("itemType") short itemType, @PathVariable("itemId") long itemId, HttpServletRequest request) {
		ItemTypeEnumForSource itemTypeEnum = ItemTypeEnumForSource.getItemTypeEnumByCode(itemType);
		if (itemTypeEnum == ItemTypeEnumForSource.COMMENT) {
			EnhancedComment enhancedComment = commentService.getEnhancedComment(itemId, null);
			if (enhancedComment != null) {
				request.setAttribute("comment", CommonConvent.convent2CommentData(enhancedComment));
			}
			return "web/expose/comment_expose";
		}
		// TODO else 继续增加类型
		return null;
	}

	/**
	 * 增加举报
	 * 
	 * @param expose
	 * @return
	 */
	@RequestMapping("add")
	public @ResponseBody ResponseData add(Expose expose) {
		ResponseData responseData = new ResponseData();
		try {
			UserContext userContext = super.getUserContext();
			exposeService.add(expose, userContext);
		} catch (UserContextException e) {
			if (e.getResponse() != null) {
				responseData.setResponseEnum(e.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(e.getMessage());
			}
		}

		return responseData;
	}
}
